import javax.rmi.CORBA.Util;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 登录界面的操作
 */


public class LoginJFrame extends JFrame implements ActionListener {
    private JLabel username,password;
    private JTextField text1,text2;
    private JButton jbutton;
    private Font y = new Font("宋体",Font.BOLD,20);  //文本字体

    public LoginJFrame(){
        super("登录界面");

        this.setSize(300, 200);
        this.setLocationRelativeTo(null);//窗体居中显示
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.getContentPane().setLayout(new FlowLayout());


        ImageIcon background = new ImageIcon("src\\images\\bgLogin.png");
        JLabel label = new JLabel(background);
        label.setBounds(0, 0, this.getWidth(), this.getHeight());
        JPanel imagePanel = (JPanel) this.getContentPane();
        imagePanel.setOpaque(false);
        this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));

        username = new JLabel("用户名:");
        password = new JLabel("密码:   ");
        text1 = new JTextField(20);
        text2 = new JPasswordField(20);
        text1.setFont(y);
        text2.setFont(y);
        this.getContentPane().add(username);
        this.getContentPane().add(text1);
        this.getContentPane().add(password);
        this.getContentPane().add(text2);


        ImageIcon icon=new ImageIcon("src\\images\\登录.png");
        jbutton = new JButton("登录",icon);
        jbutton.setFont(y);
        jbutton.addActionListener(this::actionPerformed);
        this.getContentPane().add(jbutton,"South");
        this.setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent ae) {
        String user = text1.getText();
        String password = text2.getText();
        if (ae.getSource() == jbutton) {
            dbFunction db = new dbFunction();
            boolean flag = db.Login(user, password);
                if (!flag)
                JOptionPane.showMessageDialog(this, "登录失败!");
            else {
                JOptionPane.showMessageDialog(this, "登录成功!");
                this.dispose(); //登录成功关闭登录界面
                new mySystemJFrame();//打开我的主界面
            }
        }
    }
    public static void main(String[] args) {
        new LoginJFrame();
    }
}
